
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>进一步了解 MobX · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="project.html" />
    
    
    <link rel="prev" href="compare.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    首页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="compare.html">
            
                <a href="compare.html">
            
                    
                    状态管理器的差异
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="learn.html">
            
                <a href="learn.html">
            
                    
                    进一步了解 MobX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="project.html">
            
                <a href="project.html">
            
                    
                    项目实践
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >进一步了解 MobX</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x8FDB;&#x4E00;&#x6B65;&#x4E86;&#x89E3;-mobx">&#x8FDB;&#x4E00;&#x6B65;&#x4E86;&#x89E3; MobX</h1>
<blockquote>
<p>&#x5BF9;&#x4E8E;&#x5E94;&#x7528;&#x5F00;&#x53D1;&#x4E2D;&#x7684;&#x5E38;&#x89C1;&#x95EE;&#x9898;&#xFF0C;React &#x548C; MobX&#x90FD;&#x63D0;&#x4F9B;&#x4E86;&#x72EC;&#x7279;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x3002;React &#x63D0;&#x4F9B;&#x4E86;&#x4F18;&#x5316;UI&#x6E32;&#x67D3;&#x7684;&#x673A;&#x5236;&#xFF0C; &#x8FD9;&#x79CD;&#x673A;&#x5236;&#x5C31;&#x662F;&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x865A;&#x62DF;DOM&#x6765;&#x51CF;&#x5C11;&#x6602;&#x8D35;&#x7684;DOM&#x53D8;&#x5316;&#x7684;&#x6570;&#x91CF;&#x3002;MobX &#x63D0;&#x4F9B;&#x4E86;&#x4F18;&#x5316;&#x5E94;&#x7528;&#x72B6;&#x6001;&#x4E0E; React &#x7EC4;&#x4EF6;&#x540C;&#x6B65;&#x7684;&#x673A;&#x5236;&#xFF0C;&#x8FD9;&#x79CD;&#x673A;&#x5236;&#x5C31;&#x662F;&#x4F7F;&#x7528;&#x54CD;&#x5E94;&#x5F0F;&#x865A;&#x62DF;&#x4F9D;&#x8D56;&#x72B6;&#x6001;&#x56FE;&#x8868;&#xFF0C;&#x5B83;&#x53EA;&#x6709;&#x5728;&#x771F;&#x6B63;&#x9700;&#x8981;&#x7684;&#x65F6;&#x5019;&#x624D;&#x66F4;&#x65B0;&#x5E76;&#x4E14;&#x6C38;&#x8FDC;&#x4FDD;&#x6301;&#x662F;&#x6700;&#x65B0;&#x7684;&#x3002;</p>
</blockquote>
<p><img src="img/5.png" alt="&#x56FE;5"></p>
<h3 id="&#x5B89;&#x88C5;">&#x5B89;&#x88C5;</h3>
<p>MobX &#x7684;API&#x975E;&#x5E38;&#x7684;&#x7B80;&#x6D01;&#xFF0C;&#x9700;&#x8981;&#x4E86;&#x89E3;&#x7684;&#x5185;&#x5BB9;&#x5E76;&#x4E0D;&#x591A;&#xFF0C;&#x5982;&#x679C;&#x5728; React &#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x9700;&#x8981;&#x501F;&#x52A9; mobx-react&#x3002;</p>
<p>&#x9996;&#x5148;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x5B89;&#x88C5; MobX &#x548C; mobx-react:</p>
<pre><code>npm i MobX mobx-react
</code></pre><p>MobX &#x4E2D;&#x4F1A;&#x5927;&#x91CF;&#x4F7F;&#x7528;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x5982;&#x679C;&#x4F7F;&#x7528;ES&#xFF0C;&#x53EF;&#x4EE5;&#x501F;&#x52A9;babel&#x6765;&#x4F7F;&#x7528;ES7&#x4E2D;&#x7684;&#x88C5;&#x9970;&#x5668;&#x7279;&#x6027;&#xFF0C;&#x8FD9;&#x91CC;&#x66F4;&#x63A8;&#x8350; TypeScript</p>
<h3 id="&#x539F;&#x7406;">&#x539F;&#x7406;</h3>
<p><img src="img/3.png" alt="&#x56FE;3"></p>
<p>MobX &#x7684;&#x6D41;&#x7A0B;&#x56FE;&#x5982;&#x4E0A;&#xFF0C;&#x901A;&#x5E38;&#x662F;&#xFF1A;</p>
<ul>
<li>&#x89E6;&#x53D1; action&#xFF0C;&#x5728; action &#x4E2D;&#x4FEE;&#x6539; state</li>
<li>&#x89C2;&#x5BDF;&#x5230; state &#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#xFF1A;<ul>
<li>&#x4E3B;&#x52A8;&#x89E6;&#x53D1; React &#x7684; render &#x65B9;&#x6CD5;</li>
<li>&#x901A;&#x8FC7; computed &#x8BA1;&#x7B97;&#x5BF9;&#x5E94;&#x7684;&#x6D3E;&#x751F; state</li>
<li>&#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x5BF9;&#x5E94;&#x7684; reactions&#xFF08;&#x6BD4;&#x5982;&#xFF1A;when&#x3001;autorun&#x3001;reaction &#x7B49;&#xFF09;</li>
</ul>
</li>
</ul>
<p>&#x6709;&#x4E00;&#x70B9;&#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF1A;&#x76F8;&#x5BF9;&#x4E8E; React &#x6765;&#x8BF4;&#xFF0C;MobX &#x6CA1;&#x6709;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x7684;&#x72B6;&#x6001;&#x6811;&#xFF0C;&#x72B6;&#x6001;&#x5206;&#x6563;&#x5728;&#x5404;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;store&#x4E2D;&#x3002;MobX &#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x4F7F;&#x7528; <code>Object.defineProperty</code> &#x6765;&#x62E6;&#x622A;&#x5BF9;&#x6570;&#x636E;&#x7684;&#x8BBF;&#x95EE;&#xFF0C;&#x4E00;&#x65E6;&#x503C;&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF0C;&#x5C06;&#x4F1A;&#x8C03;&#x7528; React &#x7684; render &#x65B9;&#x6CD5;&#x6765;&#x5B9E;&#x73B0;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x89C6;&#x56FE;&#x7684;&#x529F;&#x80FD;&#x6216;&#x8005;&#x89E6;&#x53D1;&#x5BF9;&#x5E94;&#x7684; reactions &#x7B49;&#x3002;</p>
<h3 id="&#x6838;&#x5FC3;&#x6982;&#x5FF5;-observable">&#x6838;&#x5FC3;&#x6982;&#x5FF5; @observable</h3>
<p>MobX &#x4E3A;&#x73B0;&#x6709;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF08;&#x5982;&#x5BF9;&#x8C61;&#xFF0C;&#x6570;&#x7EC4;&#x548C;&#x7C7B;&#x5B9E;&#x4F8B;&#xFF09;&#x6DFB;&#x52A0;&#x4E86;&#x53EF;&#x89C2;&#x5BDF;&#x7684;&#x529F;&#x80FD;&#x3002; &#x901A;&#x8FC7;&#x4F7F;&#x7528; @observable &#x88C5;&#x9970;&#x5668;&#x6765;&#x7ED9;&#x4F60;&#x7684;&#x7C7B;&#x5C5E;&#x6027;&#x6DFB;&#x52A0;&#x6CE8;&#x89E3;&#x5C31;&#x53EF;&#x4EE5;&#x7B80;&#x5355;&#x5730;&#x5B8C;&#x6210;&#x8FD9;&#x4E00;&#x5207;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { observable } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx&quot;</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Todo</span> </span>{
    id = <span class="hljs-built_in">Math</span>.random();
    @observable title = <span class="hljs-string">&quot;&quot;</span>;
    @observable finished = <span class="hljs-literal">false</span>;
}
</code></pre>
<h3 id="&#x6838;&#x5FC3;&#x6982;&#x5FF5;-observer">&#x6838;&#x5FC3;&#x6982;&#x5FF5; @observer</h3>
<p>&#x5982;&#x679C;&#x4F60;&#x7528; React &#x7684;&#x8BDD;&#xFF0C;&#x53EF;&#x4EE5;&#x628A;&#x4F60;&#x7684;(&#x65E0;&#x72B6;&#x6001;&#x51FD;&#x6570;)&#x7EC4;&#x4EF6;&#x53D8;&#x6210;&#x54CD;&#x5E94;&#x5F0F;&#x7EC4;&#x4EF6;&#xFF0C;&#x65B9;&#x6CD5;&#x662F;&#x5728;&#x7EC4;&#x4EF6;&#x4E0A;&#x6DFB;&#x52A0; observer &#x88C5;&#x9970;&#x5668;. observer &#x7531; mobx-react &#x5305;&#x63D0;&#x4F9B;&#x7684;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, {Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {observer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;mobx-react&apos;</span>;

@observer
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TodoListView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    render() {
        <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                {this.props.todoList.todos.map(todo =&gt;
                    <span class="hljs-tag">&lt;<span class="hljs-name">TodoView</span> <span class="hljs-attr">todo</span>=<span class="hljs-string">{todo}</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{todo.id}</span> /&gt;</span>
                )}
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
            Tasks left: {this.props.todoList.unfinishedTodoCount}
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    }
}
</span></code></pre>
<h3 id="&#x6838;&#x5FC3;&#x6982;&#x5FF5;-computed">&#x6838;&#x5FC3;&#x6982;&#x5FF5; @computed</h3>
<p>&#x4F7F;&#x7528; MobX&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x5728;&#x76F8;&#x5173;&#x6570;&#x636E;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#x81EA;&#x52A8;&#x66F4;&#x65B0;&#x7684;&#x503C;&#x3002;&#x901A;&#x8FC7; @computed &#x88C5;&#x9970;&#x5668;&#x65F6;&#x8C03;&#x7528;&#x5BF9;&#x5E94;&#x6D3E;&#x751F; state &#x7684; getter / setter &#x51FD;&#x6570;&#x6765;&#x8FDB;&#x884C;&#x4F7F;&#x7528;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TodoList</span> </span>{
    @observable todos = [];
    @computed get unfinishedTodoCount() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.todos.filter(todo =&gt; !todo.finished).length;
    }
}
</code></pre>
<h3 id="&#x6838;&#x5FC3;&#x6982;&#x5FF5;-reactions">&#x6838;&#x5FC3;&#x6982;&#x5FF5; reactions</h3>
<p>Reactions &#x548C; Computed &#x5F88;&#x50CF;&#xFF0C;&#x4F46;&#x5B83;&#x4E0D;&#x662F;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x503C;&#xFF0C;&#x800C;&#x662F;&#x4F1A;&#x4EA7;&#x751F;&#x4E00;&#x4E9B;&#x526F;&#x4F5C;&#x7528;&#xFF0C;&#x6BD4;&#x5982;&#x6253;&#x5370;&#x5230;&#x63A7;&#x5236;&#x53F0;&#x3001;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x3001;&#x9012;&#x589E;&#x5730;&#x66F4;&#x65B0; React &#x7EC4;&#x4EF6;&#x6811;&#x4EE5;&#x4FEE;&#x8865;DOM&#x3001;&#x7B49;&#x7B49;&#x3002;&#x7B80;&#x800C;&#x8A00;&#x4E4B;&#xFF0C;reactions &#x5728; &#x54CD;&#x5E94;&#x5F0F;&#x7F16;&#x7A0B;&#xFF08;React&#xFF09;&#x548C; &#x547D;&#x4EE4;&#x5F0F;&#x7F16;&#x7A0B;&#xFF08;MobX&#xFF09;&#x4E4B;&#x95F4;&#x5EFA;&#x7ACB;&#x6C9F;&#x901A;&#x7684;&#x6865;&#x6881;&#x3002;</p>
<p>&#x521A;&#x5F00;&#x59CB;&#x4F7F;&#x7528; MobX &#x65F6;&#xFF0C;&#x4EBA;&#x4EEC;&#x503E;&#x5411;&#x4E8E;&#x9891;&#x7E41;&#x7684;&#x4F7F;&#x7528; reactions&#xFF0C;&#x6709;&#x4E00;&#x4E2A;&#x533A;&#x5206;&#x7684;&#x9EC4;&#x91D1;&#x6CD5;&#x5219;: &#x5982;&#x679C;&#x4F60;&#x60F3;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x88AB;&#x5176;&#x5B83; observer &#x4F7F;&#x7528;&#x7684;&#x503C;&#xFF0C;&#x8BF7;&#x4F7F;&#x7528; @computed&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x60F3;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x65B0;&#x503C;&#xFF0C;&#x800C;&#x60F3;&#x8981;&#x8FBE;&#x5230;&#x4E00;&#x4E2A;&#x6548;&#x679C;&#xFF08;&#x4F8B;&#x5982;&#x6253;&#x5370;&#x65E5;&#x5FD7;&#x3001;&#x6301;&#x4E45;&#x5316;&#x6216;&#x8005;&#x66F4;&#x65B0;UI&#x7684;&#x4EE3;&#x7801;&#xFF09;&#xFF0C;&#x8BF7;&#x4F7F;&#x7528; reactions&#x3002;</p>
<p>reactions &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6E05;&#x7406;&#x5668;&#x4EE5;&#x63D0;&#x524D;&#x53D6;&#x6D88;&#x81EA;&#x52A8;&#x8FD0;&#x884C;&#x7A0B;&#x5E8F;&#x3002;</p>
<ul>
<li>@when</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x521D;&#x59CB;&#x5316;&#x65F6;&#x4E5F;&#x4F1A;&#x81EA;&#x52A8;&#x6267;&#x884C;</span>
when(
    predicate: () =&gt; boolean,
    effect?: () =&gt; <span class="hljs-keyword">void</span>,
    options?
)
<span class="hljs-comment">// &#x5982;&#x679C;&#x6CA1;&#x63D0;&#x4F9B; effect &#x51FD;&#x6570;&#xFF0C;when &#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; Promise &#x3002;&#x5B83;&#x4E0E; async / await &#x53EF;&#x4EE5;&#x5B8C;&#x7F8E;&#x7ED3;&#x5408;&#x3002;</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">await</span> when(() =&gt; that.isVisible)
    <span class="hljs-comment">// &#x7B49;&#x7B49;..</span>
}
</code></pre>
<ul>
<li>@autorun</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x521D;&#x59CB;&#x5316;&#x65F6;&#x4E5F;&#x4F1A;&#x81EA;&#x52A8;&#x6267;&#x884C;</span>
autorun(
    () =&gt; sideEffect,
    options?
)
</code></pre>
<ul>
<li>@reaction</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-comment">// reaction &#x4E0D;&#x4F1A;&#x5728;&#x521D;&#x59CB;&#x5316;&#x65F6;&#x6267;&#x884C;&#xFF0C;&#x53EA;&#x4F1A;&#x5728;&#x5BF9;&#x5E94;&#x72B6;&#x6001;&#x503C;&#x6539;&#x53D8;&#x7684;&#x65F6;&#x5019;&#x6267;&#x884C;</span>
reaction(
    () =&gt; data,
    (data, reaction) =&gt; { sideEffect },
    options?
)
</code></pre>
<h3 id="&#x6838;&#x5FC3;&#x6982;&#x5FF5;-action">&#x6838;&#x5FC3;&#x6982;&#x5FF5; @action</h3>
<p>action &#x662F;&#x4EFB;&#x4F55;&#x7528;&#x6765;&#x4FEE;&#x6539;&#x72B6;&#x6001;&#x7684;&#x547D;&#x4EE4;&#xFF0C;&#x4F7F;&#x7528; MobX &#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x663E;&#x5F0F;&#x5730;&#x6807;&#x8BB0;&#x51FA;action&#x6240;&#x5728;&#x7684;&#x4F4D;&#x7F6E;&#x3002; &#x53EF;&#x4EE5;&#x6709;&#x52A9;&#x4E8E;&#x66F4;&#x597D;&#x7684;&#x7EC4;&#x7EC7;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x5EFA;&#x8BAE;&#x5BF9;&#x4EFB;&#x4F55;&#x4FEE;&#x6539; observables &#x6216;&#x5177;&#x6709;&#x526F;&#x4F5C;&#x7528;&#x7684;&#x51FD;&#x6570;&#x4F7F;&#x7528; @action&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span> </span>{
    @observable todos = [{
        title: <span class="hljs-string">&quot;todo&#x6807;&#x9898;&quot;</span>,
        done: <span class="hljs-literal">false</span>,
    }];
    @action changeTodoTitle({index, title}){
        <span class="hljs-keyword">this</span>.todos[index].title = title
    }
}
</code></pre>
<p>action &#x88C5;&#x9970;&#x5668;&#x9075;&#x5FAA; javascript &#x4E2D;&#x6807;&#x51C6;&#x7684;&#x7ED1;&#x5B9A;&#x89C4;&#x5219;&#x3002; &#x4F46;&#x662F; action.bound &#x53EF;&#x4EE5;&#x7528;&#x6765;&#x81EA;&#x52A8;&#x5730;&#x5C06;&#x52A8;&#x4F5C;&#x7ED1;&#x5B9A;&#x5230;&#x76EE;&#x6807;&#x5BF9;&#x8C61;&#x3002; &#x6CE8;&#x610F;&#xFF0C;&#x4E0E; action &#x4E0D;&#x540C;&#x7684;&#x662F;&#xFF0C;@action.bound &#x4E0D;&#x9700;&#x8981;&#x4E00;&#x4E2A;name&#x53C2;&#x6570;&#xFF0C;&#x540D;&#x79F0;&#x5C06;&#x59CB;&#x7EC8;&#x57FA;&#x4E8E;&#x52A8;&#x4F5C;&#x7ED1;&#x5B9A;&#x7684;&#x5C5E;&#x6027;&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x6EE1;&#x8DB3;&#x8FD9;&#x4E2A;&#x9700;&#x6C42;</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Ticker</span> </span>{
    @observable tick = <span class="hljs-number">0</span>

    @action.bound
    increment() {
        <span class="hljs-keyword">this</span>.tick++ <span class="hljs-comment">// &apos;this&apos; &#x6C38;&#x8FDC;&#x90FD;&#x662F;&#x6B63;&#x786E;&#x7684;</span>
    }
}

<span class="hljs-keyword">const</span> ticker = <span class="hljs-keyword">new</span> Ticker()
setInterval(ticker.increment, <span class="hljs-number">1000</span>)
</code></pre>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5F15;&#x5165; MobX &#x5B9A;&#x4E49;&#x7684;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#xFF0C;&#x5F3A;&#x5236;&#x4F7F;&#x7528; action &#x6765;&#x4FEE;&#x6539;&#x72B6;&#x6001;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {useStrict} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;mobx&apos;</span>;
useStrict(<span class="hljs-literal">true</span>);
</code></pre>
<h3 id="&#x4E00;&#x53E5;&#x8BDD;&#x603B;&#x7ED3;">&#x4E00;&#x53E5;&#x8BDD;&#x603B;&#x7ED3;</h3>
<p>MobX &#x4E2D;&#x6700;&#x6838;&#x5FC3;&#x7684;&#x6982;&#x5FF5;&#x53EA;&#x6709;2&#x4E2A;&#xFF1A;<code>@observable</code> &#x548C; <code>@observer</code>&#xFF0C;&#x5B83;&#x4EEC;&#x5206;&#x522B;&#x5BF9;&#x5E94;&#x7684;&#x662F;&#x88AB;&#x89C2;&#x5BDF;&#x8005;&#x548C;&#x89C2;&#x5BDF;&#x8005;&#xFF0C;&#x8FD9;&#x662F;&#x5927;&#x5BB6;&#x5E38;&#x89C1;&#x7684;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#xFF0C;</p>
<hr>
<blockquote>
<p>Primer MobX - &#x7D2B;&#x8BAF;&#x7814;&#x53D1;&#x90E8;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="compare.html" class="navigation navigation-prev " aria-label="Previous page: 状态管理器的差异">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="project.html" class="navigation navigation-next " aria-label="Next page: 项目实践">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"进一步了解 MobX","level":"1.3","depth":1,"next":{"title":"项目实践","level":"1.4","depth":1,"path":"project.md","ref":"project.md","articles":[]},"previous":{"title":"状态管理器的差异","level":"1.2","depth":1,"path":"compare.md","ref":"compare.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"learn.md","mtime":"2018-09-17T17:29:54.200Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-09-17T17:30:46.911Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

